package com.mm.controller.web.taxi;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.mm.base.annotation.VerifyToken;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import com.mm.domain.taxi.DMiniUserInfo;
import com.mm.serivce.taxi.IDMiniUserInfoService;
import com.mm.vo.mp.PayOrderReq;
import com.mm.vo.web.user.DownTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 小程序用户信息表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-13
 */
@Api(value = "/mini/v1", tags = {"小程序用户信息"})
@Slf4j
@VerifyToken
@RestController
@RequestMapping("/mini/v1")
@AopLog(type = "小程序用户信息", stackTraceOnErr = true)
public class DMiniUserInfoController {

    private final IDMiniUserInfoService miniUserInfoService;

    public DMiniUserInfoController(IDMiniUserInfoService miniUserInfoService) {
        this.miniUserInfoService = miniUserInfoService;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "page", value = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "limit", value = "10"),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "type", value = "0"),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "mName", value = "")
    })
    @ApiOperation(value = "小程序用户列表查询接口", notes = "", httpMethod = "POST")
    @PostMapping("/queryMiniUserPage")
    public DResult queryMiniUserPage(@RequestParam int page, @RequestParam int limit, @RequestParam int type, @RequestParam(required = false) String mName){
        Page<DMiniUserInfo> miniUserInfoPage = miniUserInfoService.queryMiniUserPage(page, limit, type, mName);
        DPage<DMiniUserInfo> miniUserInfoDPage = DPage.to(miniUserInfoPage);
        return DResult.of(miniUserInfoDPage);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "type", value = "")
    })
    @ApiOperation(value = "小程序用户下拉列表查询接口", notes = "", httpMethod = "GET")
    @GetMapping("/queryMiniUser")
    public DResult queryMiniUser(@RequestParam int type){
        List<DMiniUserInfo> userList = miniUserInfoService.queryMiniUserAll(type);
        List<DownTreeVo> downTreeVos = new ArrayList<>();
        Optional.ofNullable(userList).orElse(new ArrayList<>())
                .forEach(a -> {
                    downTreeVos.add(DownTreeVo.builder().id(a.getMId())
                            .title(a.getOpenId() + "-" + a.getMName() ).build());
                });
        return DResult.of(downTreeVos);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "mId", value = "")
    })
    @ApiOperation(value = "小程序用户列表查询单个接口", notes = "", httpMethod = "POST")
    @PostMapping("/queryMiniUserOne")
    public DResult queryMiniUserOne(@RequestParam Integer mId){
        return DResult.of(miniUserInfoService.queryMiniUserOne(mId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DMiniUserInfo", dataTypeClass = Integer.class, name = "miniUserInfo", value = "")
    })
    @ApiOperation(value = "小程序用户列表编辑接口", notes = "", httpMethod = "POST")
    @Transactional
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DMiniUserInfo miniUserInfo) {
        boolean flag = false;
        if (miniUserInfo.getMId() < 0) {
            // 新增
            flag = miniUserInfoService.save(miniUserInfo);
        } else if (miniUserInfo.getMId()  > 0) {
            // 更新
            flag = miniUserInfoService.updateById(miniUserInfo);
        }
        return DResult.of(flag);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "mId", value = "")
    })
    @ApiOperation(value = "小程序用户删除接口", notes = "", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer mId){
        boolean flag = false;
        if (mId > 0) {
            flag = miniUserInfoService.removeMiniUser(mId);
        }
        return DResult.of(flag);
    }


}
